package com.ssm.hj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.hj.page.Page;
import com.ssm.hj.search.MemberSearch;
import com.ssm.hj.service.InterposeSchemeService;
import com.ssm.lzh.vo.InterposeSchemeVo;
import com.ssm.hj.view.InterposeSchemeView;
import com.ssm.hj.view.MouldMap;
import com.ssm.mapper.InterposeSchemeMapper;
import com.ssm.pojo.Member;
import com.ssm.pojo.Mould;
import com.ssm.pojo.TInterposeScheme;
import com.ssm.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


@Service
public class InterposeSchemeServiceImpl implements InterposeSchemeService {
    @Resource
    private InterposeSchemeMapper interposeSchemeMapper;


    @Override
    public PageInfo<Member> queryMemberGroupClassificationService(Page<MemberSearch> page) throws Exception {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return new PageInfo<Member>(this.interposeSchemeMapper.queryMemberGroupClassification(page.getSearchObj()));
    }

    @Override
    public InterposeSchemeView queryBookIngByIdService(int rid) throws Exception {
        return this.interposeSchemeMapper.queryBookIngById(rid);
    }

    @Override
    public List<Mould> queryMouldAllService() throws Exception {
        return this.interposeSchemeMapper.queryMouldAll();
    }

    @Override
    public MouldMap queryMouldByIdService(int id) throws Exception {
        MouldMap mouldMap = this.interposeSchemeMapper.queryMouldFoodById(id);
        mouldMap.setExercises(this.interposeSchemeMapper.queryMouldExerciseById(id));
        return mouldMap;
    }

    @Override
    public int addOrUpdateTInterposeSchemeService(TInterposeScheme tInterposeScheme) throws Exception {
        int i = this.interposeSchemeMapper.queryTInterposeSchemeById(tInterposeScheme.getRid());
        if (i > 0) {
            return this.interposeSchemeMapper.updateTInterposeScheme(tInterposeScheme);
        } else {
            return this.interposeSchemeMapper.addTInterposeScheme(tInterposeScheme);
        }
    }

    @Override
    public Map<String, InterposeSchemeVo> queryt_interpose_scheme(int id, String d1, String d2) {
        List<InterposeSchemeVo> gyfan =
                this.interposeSchemeMapper.queryt_interpose_scheme(id, d1, d2);
        List<InterposeSchemeVo> gyhf =
                this.interposeSchemeMapper.querytt_interpose_followup_visit(id, d1, d2);


        Map<String, InterposeSchemeVo> map = new HashMap<String, InterposeSchemeVo>();

        for (InterposeSchemeVo v : gyfan
        ) {
            for (InterposeSchemeVo vv : gyhf
            ) {
                if (v.getTime().equals(vv.getTime())) {
                    v.setHysum(vv.getHysum());
                    map.put(v.getTime(), v);
                } else {
                    map.put(vv.getTime(), vv);
                }
            }

        }
        return map;
    }


    @Override
    public int updateStateService(int rid) throws Exception {
        try {
            this.interposeSchemeMapper.updateTInterposeSchemeState(rid);
            this.interposeSchemeMapper.updateBookingState(rid);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }
}
